<ui:composition xmlns="http://www.w3.org/1999/xhtml"
	xmlns:ui="http://java.sun.com/jsf/facelets"
	xmlns:h="http://java.sun.com/jsf/html"
	xmlns:f="http://java.sun.com/jsf/core"
	xmlns:p="http://primefaces.org/ui"
	template="../templates/ui.xhtml">
    
    <ui:define name="head">
        <style type="text/css">
        </style>
        
        <script type="text/javascript">
            //<![CDATA[
function start() {
    statusDialog.show();
}

function stop() {
    statusDialog.hide();
}
            //]]>
        </script>
    </ui:define>

	<ui:define name="content">

		<h1 class="title ui-widget-header ui-corner-all">FileDownload</h1>
		<div class="entry">
			<p>FileDownload is used to stream binary contents like files stored in database to the client. FileDownload
			is used by attaching it to any JSF command component like button or a link. Additionally presentation
			of download can be configured with the contentDisposition attribute that takes either "attachment" or "inline" as a value.</p>
			
            <p:dialog modal="true" widgetVar="statusDialog" header="Status" draggable="false" closable="false" resizable="false">
				<p:graphicImage value="/design/ajaxloadingbar.gif" />
			</p:dialog>
            
			<h:form id="form">
			
				<p:commandButton id="downloadLink" value="Download" ajax="false" onclick="PrimeFaces.monitorDownload(start, stop)" icon="ui-icon-arrowthick-1-s">
					<p:fileDownload value="#{fileDownloadController.file}" />
				</p:commandButton>
			
			</h:form>
						
			<h3>Source</h3>
            <p:tabView>
                <p:tab title="fileDownload.xhtml">
                    <pre name="code" class="xml">
&lt;p:dialog modal="true" widgetVar="statusDialog" header="Status" draggable="false" closable="false" resizable="false"&gt;
    &lt;p:graphicImage value="/design/ajaxloadingbar.gif" /&gt;
&lt;/p:dialog&gt;

&lt;h:form id="form"&gt;

&lt;p:commandButton id="downloadLink" value="Download" ajax="false" onclick="PrimeFaces.monitorDownload(start, stop)" 
        icon="ui-icon-arrowthichk-s"&gt;
    &lt;p:fileDownload value="\#{fileDownloadController.file}" /&gt;
&lt;/p:commandButton&gt;

&lt;/h:form&gt;

        &lt;script type="text/javascript"&gt;
function start() {
    statusDialog.show();
}

function stop() {
    statusDialog.hide();
}
        &lt;/script&gt;
                    </pre>  
                </p:tab>
                
                <p:tab title="FileDownloadController.java">
                    <pre name="code" class="java">						
package org.primefaces.examples.view;

import java.io.InputStream;
import javax.faces.context.FacesContext;
import javax.servlet.ServletContext;

import org.primefaces.model.DefaultStreamedContent;
import org.primefaces.model.StreamedContent;

public class FileDownloadController {

	private StreamedContent file;
	
	public FileDownloadController() {        
        InputStream stream = ((ServletContext)FacesContext.getCurrentInstance().getExternalContext().getContext()).getResourceAsStream("/images/optimusprime.jpg");
		file = new DefaultStreamedContent(stream, "image/jpg", "downloaded_optimus.jpg");
	}

    public StreamedContent getFile() {
        return file;
    }  
}
                    </pre>
                </p:tab>
            </p:tabView>

		</div>

	</ui:define>
</ui:composition>
